using System;
using System.Collections.Generic;
using System.Xml.Linq;
using MediaOwl.LastFm.Resources;

namespace MediaOwl.LastFm.Data
{
    /// <summary>
    /// Represents a Search delivered from Last.fm
    /// </summary>
    public class Search
    {
        public Search(Type searchFor, IEnumerable<XElement> openSearchXmlData)
        {
            SearchForType = searchFor;
            FromXml(openSearchXmlData);
        }

        public Type SearchForType { get; private set; }
        public string SearchTerms { get; set; }
        public int StartPage { get; set; }
        public long TotalResults { get; set; }
        public int StartIndex { get; set; }
        public int ItemsPerPage { get; set; }
        public string SearchResultText
        {
            
            get
            {
                if (string.IsNullOrWhiteSpace(SearchTerms) && TotalResults == 0)
                    return string.Empty;

                long totalTemp = StartPage * ItemsPerPage > TotalResults ? TotalResults : StartPage * ItemsPerPage;

                //Results {0} to {1} of {2} are shown;
                var s = string.Format(Captions.SearchResultBaseText,
                    (StartPage - 1) * ItemsPerPage + 1,
                    totalTemp,
                    TotalResults);

                return string.IsNullOrWhiteSpace(SearchTerms)
                           ? s
                           : string.Format(Captions.SearchTermBaseText, SearchTerms) + " " + s;
            }
        }

        public void FromXml(IEnumerable<XElement> openSearchXmlData)
        {
            foreach (var xElement in openSearchXmlData)
            {
                //<opensearch:Query role="request" searchTerms="cher" startPage="1"/>

                if (xElement.Name.LocalName == "Query")
                {
                    if (xElement.Attribute("searchTerms") != null)
                    {
                        // ReSharper disable PossibleNullReferenceException
                        SearchTerms = xElement.Attribute("searchTerms").Value;
                        // ReSharper restore PossibleNullReferenceException
                    }
                    if (xElement.Attribute("startPage") != null)
                    {
                        // ReSharper disable PossibleNullReferenceException
                        StartPage = Convert.ToInt32(xElement.Attribute("startPage").Value);
                        // ReSharper restore PossibleNullReferenceException
                    }
                    
                }

                if (xElement.Name.LocalName == "totalResults")
                    TotalResults = Convert.ToInt32(xElement.Value);

                if (xElement.Name.LocalName == "startIndex")
                    StartIndex = Convert.ToInt32(xElement.Value);

                if (xElement.Name.LocalName == "itemsPerPage")
                    ItemsPerPage = Convert.ToInt32(xElement.Value);
            }
        }
    }
}